package org.example;

import java.util.ArrayList;
import java.util.List;

public class Test2 {
    //leetcode分割回文串
    //https://leetcode.cn/problems/palindrome-partitioning/description/?envType=problem-list-v2&envId=dynamic-programming&difficulty=MEDIUM
    List<List<String>> ret;
    List<String> path;
    public List<List<String>> partition(String s) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        backTracking(s,0);
        return ret;
    }

    private void backTracking(String s,int start) {
        if (start >= s.length()) {
            ret.add(new ArrayList<>(path));
            return;
        }
        for (int i = start; i < s.length(); i++) {
            String ss = s.substring(start,i+1);
            if (isPalindrome(ss)) {
                path.add(ss);
            }else {
                continue;
            }
            backTracking(s,i+1);
            path.remove(path.size()-1);
        }
    }

    private boolean isPalindrome(String s) {
        for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}
